package cn.yshujia.handler.sensitive;


import cn.yshujia.encryption.AESEncrypt;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author：yshujia
 * @create: 2025/4/14 12:27
 * @description: AbstractSensitiveHandler 敏感数据处理抽象类
 */
public abstract class AbstractSensitiveHandler extends BaseTypeHandler<String> {
	
	public abstract String set(String str);
	
	@Override
	public void setNonNullParameter(PreparedStatement preparedStatement, int i, String s, JdbcType jdbcType) throws SQLException {
		preparedStatement.setString(i, set(s));
	}
	
	@Override
	public String getNullableResult(ResultSet resultSet, String s) throws SQLException {
		return AESEncrypt.decrypt(resultSet.getString(s));
	}
	
	@Override
	public String getNullableResult(ResultSet resultSet, int i) throws SQLException {
		return AESEncrypt.decrypt(resultSet.getString(i));
	}
	
	@Override
	public String getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
		return AESEncrypt.decrypt(callableStatement.getString(i));
	}
}
